package Grantsfire;

use strict;
use warnings;

use FormValidator::Simple;
use HTML::Entities;

use Catalyst qw/
    DefaultEnd 
    ConfigLoader 
    Static::Simple 
    FillInForm Dumper
    Authentication
    Authentication::Store::Minimal
    Authentication::Credential::HTTP
	Cache::FileCache
/;

our $VERSION = '0.02';

#
#  Load conf/grantsfire.yml and conf/grantsfire_local.yml
#
print "Loading configuration...\n";
__PACKAGE__->config( 'Plugin::ConfigLoader' => { file => __PACKAGE__->path_to('conf')  } );
__PACKAGE__->setup;

require Grantsfire::Schema::RuntimeMixins;

FormValidator::Simple->set_messages( {
    DEFAULT => {
        url => {
            NOT_BLANK => 'URL is blank',
            HTTP_URL => 'Invalid HTTP(S) URL',
        },
    },
});

sub check_params {
    my $c = shift;
    my $constraints = shift;
    my $result = FormValidator::Simple->check($c->req, $constraints);
    die $result->messages($c->req->action)->[0] . "\n" if $result->has_error;
}

sub redirect_success {
    my $c = shift;
    @_ = (success => $_[0]) if @_ == 1;
    my %params = @_;
    my $uri = URI->new(delete $params{uri} || $c->request->headers->header('Referer'));
    delete $params{success} if scalar @{$c->error};
    $uri->query_form(\%params);
    $c->res->redirect($uri);
}

sub end : Private {
    my ($self, $c) = @_;
    #for ($c->req->headers->header('Accept')) {
    #    if (/\bapplication\/xhtml\+xml\b/mis) {
    #        $c->res->content_type('application/xhtml+xml');
    #    }
    #}
    if ($c->res->redirect) {
        my $redirect = URI->new($c->res->redirect);
        if (scalar @{ $c->error }) {
            $redirect->query_param('error', @{ $c->error });
            foreach my $k ($c->req->param) {
                $redirect->query_param($k, $c->req->param($k));
            }
            $c->res->redirect($redirect);
            $c->error(0);
        }
        else {
            $c->res->redirect($redirect);
        }
    }
    $self->SUPER::end($c);
    if ($c->stash->{this_is_a_form}) {
        $c->fillform($c->req->params);
    }
}

sub title { 
    my $c = shift;
    my @title = map(encode_entities("\u$_"), grep($_ ne "", $c->req->uri->path_segments));
    return @title ? $c->config->{name} . ' &raquo; ' . join(' ', @title) : "Grantsfire: A New Way to Share Grants Knowledge";
}
sub pageid { 
    my $c = shift;
    my @path = grep length, map({s/-/_/g; s/[^A-Za-z_]//g; $_}  $c->req->uri->path_segments);
    my @action = grep length, map({s/-/_/g; s/[^A-Za-z_]//g; $_} split('/', $c->req->action));
    return (@action and $action[0] ne 'default') ? join('_', @action) : (@path ? join('_', @path) : 'default');
}

#
# IMPORTANT: Please look into Grantsfire::Controller::Root for more
#

=head1 NAME

Grantsfire - Catalyst based application

=head1 SYNOPSIS

    script/grantsfire_server.pl

=head1 DESCRIPTION

Catalyst based application.

=head1 SEE ALSO

L<Grantsfire::Controller::Root>, L<Catalyst>

=head1 AUTHOR

Keith Grennan

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
